package IC.lir.Instrunction;

public class ArrayMemory extends Memory {

	private Register _arrayObj;
	private Data _arrayIndex;
	
	public ArrayMemory(Register arrayObj, Storable arrayIndex)
	{
		_arrayObj = arrayObj;
		_arrayIndex = arrayIndex; 
	}
	
	public ArrayMemory(Register arrayObj, VaribleMemory arrayIndex)
	{
		_arrayObj = arrayObj;
		_arrayIndex = arrayIndex; 
	}

	public Instruction Load(int registerNum)
	{
		return Load(null);
	}
	public Instruction Load(Register toWhere)
	{
		return new ArrayAccessInstruction(this, toWhere == null ? GetStoreRegister(0) : toWhere); 
	}
	public Register GetStoreRegister(int registerNum)
	{
		if (!(_arrayIndex instanceof Register))
			return new Register (_arrayObj.GetIndex());
		
		return new Register(
			java.lang.Math.min(((Register)_arrayIndex).GetIndex(), _arrayObj.GetIndex()));
	}
	
	public Instruction Store(Storable what) { return new ArrayStoreInstruction(this, what); }
	
	public int GetNumOfRegisters() { return _arrayIndex instanceof Register ? 2 : 1; }
	
	public String toString()
	{ 
		return String.format("%s[%s]", _arrayObj.toString(), _arrayIndex.toString()); 
	}
}